<?php
class ImportData {
	
	/**
	 * 插入品牌，如果已存在则返回id
	 * @param 品牌名  $name
	 * @param 首字母  $firstLetter
	 * @return 插入或查找到的id
	 */
	public function importBrand($name, $firstLetter) {
		$brandModel = new BrandModel();
		$option = array("name"=>$name);
		$id  =  $brandModel->select(array("id"), $option)[0]["id"];
		if( !$id) {
			$id = $brandModel->insert(array("name"=>$name, "first_letter"=>$firstLetter));
		} 
		return $id;
	}
	
	public function importMaker($name, $brand_id) {
		$makerModel = new MakerModel();
		$option = array("name"=>$name);
		$id = $makerModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $makerModel->insert(array("name"=>$name, "brand_id"=>$brand_id));
		}
		return $id;
	}
	
	public function importSeries($name, $maker_id) {
		$seriesModel = new SeriesModel();
		$option = array("name"=>$name);
		$id = $seriesModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $seriesModel->insert(array("name"=>$name, "maker_id"=>$maker_id));
		}
		return $id;
	}
	
	public function importCountry($name) {
		$countryModel = new CountryModel();
		$option = array("name"=>$name);
		$id = $countryModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $countryModel->insert(array("name"=>$name));
		}
		return $id;
	}
	
	public function importClass($name) {
		$classModel = new ClassModel();
		$option = array("name"=>$name);
		$id = $classModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $classModel->insert(array("name"=>$name));
		}
		return $id;
	}
	
	public function importTechnology($name) {
		$technologyModel = new TechnologyModel();
		$option = array("name"=>$name);
		$id = $technologyModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $technologyModel->insert(array("name"=>$name));
		}
		return $id;
	}
	
	public function importTransmission($name) {
		$transMissionModel = new TransmissionModel();
		$option = array("name"=>$name);
		$id = $transMissionModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $transMissionModel->insert(array("name"=>$name));
		}
		return $id;
	}
	
	public function importMobileType($name) {
		$mobileTypeModel = new MobileTypeModel();
		$option = array("name"=>$name);
		$id = $mobileTypeModel->select(array("id"), $option)[0]["id"];
		if( !$id ) {
			$id = $mobileTypeModel->insert(array("name"=>$name));
		}
		return $id;
	}
	
	public function importAutoMobile($name, $mobileType_id, $country_id,
					$technology_id, $class_id, $displacement, $engine,
					$transmission_id, $begin_date, $end_date, $brand_id,
					$maker_id, $series_id) {
		
		$mobileModel = new MobileModel();
		$option = array("name"=>$name);
		$id = $mobileModel->select(array("id"), $option, "limit 0, 1")[0]["id"];
		
		$mobileModel->mobiletype_id = $mobileType_id;
		$mobileModel->country_id = $country_id;
		$mobileModel->technology_id = $technology_id;
		$mobileModel->class_id = $class_id;
		$mobileModel->transmission_id = $transmission_id;
		$mobileModel->displacement = $displacement;
		$mobileModel->engine = $engine;
		$mobileModel->begin_date = $begin_date;
		$mobileModel->end_date = $end_date;
		$mobileModel->brand_id = $brand_id;
		$mobileModel->maker_id = $maker_id;
		$mobileModel->series_id = $series_id;
		if( $id ) {
			$mobileModel->update("id = $id");
		}
		else {
			// _print($name);
			$mobileModel->name = $name;
			$id = $mobileModel->insert();
		}
		return $id;
	}
}